//
//  HLRecordListCell.m
//  YueYue
//
//  Created by hulu001 on 2023/6/15.
//

#import "HLRecordListCell.h"

@interface HLRecordListCell ()

@property (nonatomic, strong) UIImageView *avatarView;
@property (nonatomic, strong) UILabel *nicknameLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) QMUIButton *chatButton;

@end

@implementation HLRecordListCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = UIColorWhite;
        [self setupUI];
    }
    return self;
}


- (void)setupUI
{
    self.avatarView = [[UIImageView alloc] init];
    _avatarView.layer.cornerRadius = 8;
    _avatarView.layer.masksToBounds = YES;
    _avatarView.image = [HLUIHelper avatarPlaceholder];
    [self.contentView addSubview:_avatarView];
    [_avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(56);
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(4);
        make.bottom.mas_equalTo(-6);
    }];
    
    
    self.nicknameLabel = [[UILabel alloc] init];
    _nicknameLabel.textColor = HLTextColor1;
    _nicknameLabel.text = @"聊人生阿姨有没有啊";
    _nicknameLabel.font = UIFontMediumMake(18);
    [self.contentView addSubview:_nicknameLabel];
    [_nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_avatarView.mas_right).offset(12);
        make.top.equalTo(_avatarView).offset(4);
        make.right.mas_equalTo(-104);
    }];
    
    self.contentLabel = [[UILabel alloc] init];
    _contentLabel.font = UIFontMake(14);
    _contentLabel.textColor = [UIColor qmui_colorWithHexString:@"#A2A6B0"];
    _contentLabel.numberOfLines = 1;
    _contentLabel.text = @"错误的决定比没有决定要好";
    [self.contentView addSubview:_contentLabel];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nicknameLabel);
        make.bottom.equalTo(_avatarView).offset(-2);
        make.right.mas_equalTo(-104);
    }];
    
    
    self.chatButton = [[QMUIButton alloc] init];
    [_chatButton setImage:UIImageMake(@"record_list_video") forState:UIControlStateNormal];
    [_chatButton addTarget:self action:@selector(chatAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_chatButton];
    [_chatButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-16);
    }];
}


- (void)setModel:(HLRecordModel *)model
{
    _model = model;
    
    [_avatarView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[HLUIHelper avatarPlaceholder]];
    _nicknameLabel.text = model.nickName;
    
    NSString *durationStr = model.durationStr;
    if ([self isEmptyValue:durationStr]) {
        durationStr = @"00:00";
    }
    _contentLabel.text = [NSString stringWithFormat:@"%@ | 时长 %@", [HLHelper showDateString:model.latelyCallTime], model.durationStr];
    
}

- (BOOL)isEmptyValue:(id)value
{
    if (!value || [value isKindOfClass:[NSNull class]] || value == NULL)
    {
        return YES;
    }
    else if ([value isKindOfClass:[NSString class]])
    {
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *trimedString = [value stringByTrimmingCharactersInSet:set];
        if ([trimedString length] == 0 || [value isEqualToString:@"<null>"] || [value isEqualToString:@"null"] || [value isEqualToString:@"(null)"]) {
            return YES;
        } else {
            return NO;
        }
    }
    else if ([value isKindOfClass:[NSNumber class]])
    {
        return NO;
    }
    return YES;
}

- (void)chatAction
{
    HLUserInfoModel *user = [[HLUserInfoModel alloc] init];
    user.userId = self.model.userId;
    [HLUIHelper callWithUser:user];
}


@end
